<template>
  <div class="register-container">
    <!-- 注册页面背景 -->
    <div class="background-decoration"></div>
    
    <div class="register-box">
      <!-- 应用logo和标题 -->
      <div class="register-header">
        <div class="logo">
          <i class="el-icon-food"></i>
        </div>
        <h2 class="register-title">校园外卖平台</h2>
        <p class="register-subtitle">加入我们，享受美食之旅</p>
      </div>
      
      <!-- 注册表单 -->
      <el-form :model="registerForm" :rules="registerRules" ref="registerForm" label-width="0px">
        <el-form-item prop="username">
          <el-input 
            v-model="registerForm.username" 
            prefix-icon="el-icon-user" 
            placeholder="请输入用户名"
            :clearable="true">
          </el-input>
        </el-form-item>
        <el-form-item prop="realName">
          <el-input 
            v-model="registerForm.realName" 
            prefix-icon="el-icon-user" 
            placeholder="请输入真实姓名"
            :clearable="true">
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input 
            v-model="registerForm.password" 
            prefix-icon="el-icon-lock" 
            placeholder="请输入密码" 
            show-password>
          </el-input>
        </el-form-item>
        <el-form-item prop="confirmPassword">
          <el-input 
            v-model="registerForm.confirmPassword" 
            prefix-icon="el-icon-lock" 
            placeholder="请确认密码" 
            show-password>
          </el-input>
        </el-form-item>
        <el-form-item prop="phone">
          <el-input 
            v-model="registerForm.phone" 
            prefix-icon="el-icon-mobile-phone" 
            placeholder="请输入手机号"
            :clearable="true">
          </el-input>
        </el-form-item>
        <el-form-item prop="email">
          <el-input 
            v-model="registerForm.email" 
            prefix-icon="el-icon-message" 
            placeholder="请输入邮箱"
            :clearable="true">
          </el-input>
        </el-form-item>
        
     
        <el-form-item>
          <el-button type="primary" :loading="loading" @click="handleRegister" class="register-button">
            <i class="el-icon-plus" v-if="!loading"></i>
            立即注册
          </el-button>
        </el-form-item>
        
        <!-- 额外选项 -->
        <div class="register-options">
          <span>已有账号?</span>
          <el-link type="primary" @click="goToLogin" class="login-link">去登录</el-link>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import { register, checkUsername, checkPhone, checkEmail } from '@/api/user'
import { isValidPhone, isValidEmail } from '@/utils'

export default {
  name: 'Register',
  data() {
    // 自定义验证规则
    const validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.registerForm.password) {
        callback(new Error('两次输入密码不一致'))
      } else {
        callback()
      }
    }
    
    const validatePhone = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入手机号'))
      } else if (!isValidPhone(value)) {
        callback(new Error('请输入正确的手机号'))
      } else {
        callback()
      }
    }
    
    const validateEmail = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入邮箱'))
      } else if (!isValidEmail(value)) {
        callback(new Error('请输入正确的邮箱'))
      } else {
        callback()
      }
    }
    
    return {
      // 注册表单
      registerForm: {
        username: '',
        password: '',
        confirmPassword: '',
        phone: '',
        email: '',
        realName: '',
        role: 1 // 默认为学生
      },
      // 表单验证规则
      registerRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        realName: [
          { required: true, message: '请输入真实姓名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: '请确认密码', trigger: 'blur' },
          { validator: validatePass2, trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { validator: validatePhone, trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { validator: validateEmail, trigger: 'blur' }
        ]
      },
      // 注册按钮加载状态
      loading: false
    }
  },
  methods: {
    // 处理注册
    handleRegister() {
      this.$refs.registerForm.validate(async valid => {
        if (valid) {
          this.loading = true
          
          try {
            // 提交注册信息
            const registerData = {
              username: this.registerForm.username,
              password: this.registerForm.password,
              phone: this.registerForm.phone,
              email: this.registerForm.email,
              confirmPassword: this.registerForm.confirmPassword,
              role: this.registerForm.role, // 用户类型：1为学生，2为商家
              realName: this.registerForm.realName
            }
            
            const response = await register(registerData)
            
            if (response.code === 200) {
              this.$message({
                message: '注册成功，欢迎加入！',
                type: 'success',
                duration: 2000
              })
              this.$router.push('/login')
            } else {
              this.$message.error(response.message || '注册失败')
            }
          } catch (error) {
            this.$message.error(error.response?.message || '注册失败，请稍后再试')
          } finally {
            this.loading = false
          }
        }
      })
    },
    // 跳转到登录页
    goToLogin() {
      this.$router.push('/login')
    }
  }
}
</script>

<style scoped>
/* 注册页面容器 */
.register-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f5f7fa;
  position: relative;
  overflow: hidden;
}

/* 背景装饰 */
.background-decoration {
  position: absolute;
  top: -50%;
  right: -50%;
  width: 100%;
  height: 200%;
  background: linear-gradient(45deg, #409EFF20, #67C23A20);
  transform: rotate(-45deg);
  z-index: 0;
}

/* 注册框 */
.register-box {
  width: 420px;
  padding: 40px;
  background-color: #fff;
  border-radius: 12px;
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.1);
  position: relative;
  z-index: 1;
  transition: all 0.3s ease;
  max-height: 90vh;
  overflow-y: auto;
}

.register-box:hover {
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.15);
  transform: translateY(-5px);
}

/* 注册头部区域 */
.register-header {
  text-align: center;
  margin-bottom: 25px;
}

/* Logo样式 */
.logo {
  margin: 0 auto 15px;
  height: 60px;
  width: 60px;
  line-height: 60px;
  font-size: 35px;
  color: white;
  background: linear-gradient(135deg, #409EFF, #67C23A);
  border-radius: 50%;
  box-shadow: 0 4px 10px rgba(64, 158, 255, 0.3);
}

/* 注册标题 */
.register-title {
  font-size: 24px;
  font-weight: 600;
  margin-bottom: 5px;
  color: #303133;
}

/* 注册副标题 */
.register-subtitle {
  font-size: 14px;
  color: #909399;
  margin-top: 0;
}

/* 注册按钮 */
.register-button {
  width: 100%;
  height: 45px;
  font-size: 16px;
  letter-spacing: 2px;
  border-radius: 4px;
  background: linear-gradient(90deg, #409EFF, #67C23A);
  border: none;
  transition: all 0.3s;
}

.register-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(64, 158, 255, 0.4);
}

/* 用户类型选择器 */
.user-type-selector {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}

.selector-label {
  margin-right: 10px;
  color: #606266;
}

/* 注册选项区域 */
.register-options {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 20px;
}

.register-options span {
  color: #606266;
  margin-right: 5px;
}

/* 登录链接 */
.login-link {
  font-weight: 500;
}

/* 输入框样式优化 */
:deep(.el-input__inner) {
  height: 45px;
  line-height: 45px;
  border-radius: 4px;
}

:deep(.el-input__prefix) {
  left: 10px;
}

:deep(.el-input__inner:focus) {
  border-color: #409EFF;
  box-shadow: 0 0 5px rgba(64, 158, 255, 0.2);
}

/* 自适应移动端 */
@media screen and (max-width: 768px) {
  .register-box {
    width: 90%;
    padding: 25px;
  }
}
</style> 